#!/usr/bin/perl

# Sweeps name
$name = "sim";

# Sweeps replicates
$replicates_per_sweep = 3;

# Neutral microsatellite mut rate is 10^-4, 10^-5, 10^-7 (literature based)
$neutral_mutation_rate[0] = 1/10000;
$neutral_mutation_rate[1] = 1/100000;
$neutral_mutation_rate[2] = 1/10000000;

# Mutation rate of the selective loci
# We have 10^-5 .. 10^-8
# 9 loci total, 3 survs, 3 repros, 1 mutators
$selective_mutation_rate[0] = 1/100000;
$selective_mutation_rate[1] = 1/1000000;
$selective_mutation_rate[2] = 1/10000000;
$selective_mutation_rate[3] = 1/100000000;


# Sweep selective coefficient
$selective_coefficient[0] = 0.001;
$selective_coefficient[1] = 0.01;
$selective_coefficient[2] = 0.1;
$selective_coefficient[3] = 0.5;
$selective_coefficient[4] = 1.0;
$selective_coefficient[5] = 2.0;

# division prob
$crypt_div_prob[0] = 0.0;
$crypt_div_prob[1] = 0.25;
$crypt_div_prob[2] = 0.5;
$crypt_div_prob[3] = 0.75;
$crypt_div_prob[4] = 1.0;

# multidim
my $data;
my $fname = 0;
# Sweep div prob
for $divprob (0..4) {
    # Sweep selective coefficient
    for $selcoeff (0..5){
	# Sweep selective mut rate
	for $selmu (0..3){
	    # Sweep neutral mut rate
	    for $neutmu (0..2){
		# Number of repro and surv hits
		for $selloci (5..5) {
		    # Repeat replicates times
		    for $replicates (0..$replicates_per_sweep-1){
			# total sweep count
			if(!defined($data[$selmu][$divprob][$selcoeff][0])){
			    $data[$selmu][$divprob][$selcoeff][0]=0;
			}
			$data[$selmu][$divprob][$selcoeff][0]++;
			# running count
			if(!defined($data[$selmu][$divprob][$selcoeff][1])){
			    $data[$selmu][$divprob][$selcoeff][1]=0;
			}
			$fname++;
			open(IN, "clones.number.alleles.frequency.analysis/$name$fname.clones.number.csv");
			while(<IN>){
			    chomp;
			    @row = split(/\,/);
			    if($row[0] eq "7300"){
				# row[2] is number of clones whose last hit was a reproductive locus
				$data[$selmu][$divprob][$selcoeff][1]+=$row[2];
			    }
			}

			close IN;			
		    }
		}
	    }
	}	
    }
}

# 6 separate figures/plots
for $selcoeff (0..5){
    # rows are the bars, rows are the selective mutation rate -5, -6, -7 -8
    for $selmu (0..3){
	# cols are div probability
	for $divprob (0..4) {
	    if($divprob<4){
		print $data[$selmu][$divprob][$selcoeff][1].",";
	    } else {
		print $data[$selmu][$divprob][$selcoeff][1];
	    }
	}
	print "\n";
    }
}

